package com.xjrsoft.module.customer.erp_model.eRP_StockInventory.service.impl;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.dbmodel.utils.DataSourceUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryDto;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryentryDto;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.entity.ErpStockinventory;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.entity.ErpStockinventoryentry;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.mapper.ErpStockinventoryentryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;

import java.awt.*;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.mapper.ErpStockinventoryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.service.IErpStockinventoryService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.vo.ErpStockinventoryentryVo;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-10-08
 */
@Service
@AllArgsConstructor
public class ErpStockinventoryServiceImpl extends BaseService<ErpStockinventoryMapper, ErpStockinventory> implements IErpStockinventoryService {

	private ErpStockinventoryentryMapper erpStockinventoryentryMapper;

	@Override
	public IPage<ErpStockinventory> getPageList(ErpStockinventoryListDto pageListDto) {
		Wrapper<ErpStockinventory> wrapper = Wrappers.<ErpStockinventory>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_InventoryCode()), ErpStockinventory::getFInventorycode, pageListDto.getF_InventoryCode())
				.ge(!StringUtil.isEmpty(pageListDto.getF_InventoryDate_Start()), ErpStockinventory::getFInventorydate, pageListDto.getF_InventoryDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_InventoryDate_End()), ErpStockinventory::getFInventorydate, pageListDto.getF_InventoryDate_End())
				.eq(!StringUtil.isEmpty(pageListDto.getF_WareHouseId()), ErpStockinventory::getFWarehouseid, pageListDto.getF_WareHouseId());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpStockinventory(ErpStockinventoryDto erpStockinventoryDto,ErpStockinventory erpStockinventory, List<ErpStockinventoryentryDto> erpStockinventoryentryDtos,List<ErpStockinventoryentry> erpStockinventoryentryList) {

		setDifQua(erpStockinventoryentryDtos,erpStockinventoryentryList);
		String erpStockinventoryId = IdWorker.get32UUID();
		erpStockinventory.setFId(erpStockinventoryId);
		erpStockinventoryentryList.forEach(erpStockinventoryentry ->{
			erpStockinventoryentry.setFInventoryid(erpStockinventoryId);
			erpStockinventoryentry.setFAuditmark(1);
			erpStockinventoryentry.setFWearhouseid(erpStockinventoryDto.getFWarehouseid());
			erpStockinventoryentry.setFWearname(erpStockinventoryDto.getFWarehousename());
		} );
		this.saveBatch(erpStockinventoryentryList, ErpStockinventoryentry.class, ErpStockinventoryentryMapper.class);
		boolean isSuccess = this.save(erpStockinventory);
		return isSuccess ? erpStockinventoryId : null;
	}
	@SneakyThrows
	public boolean setDifQua(List<ErpStockinventoryentryDto> erpStockinventoryentryDtos, List<ErpStockinventoryentry> erpStockinventoryentryList) {
			erpStockinventoryentryDtos.forEach(erpStockinventoryentryDto ->{
				//查询转换率
				String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpStockinventoryentryDto.getFUnitid()+"'";
				List<Entity>  list= null;
				try {
					list = Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);
				} catch (SQLException throwables) {
					throwables.printStackTrace();
				}
				BigDecimal baseConverRate = null;
				//显示单位转换率
				BigDecimal comConverRate=null;
				for (int j=0;j<list.size();j++)
				{
					if (list.get(j).get("f_fullname").toString().equals(erpStockinventoryentryDto.getFComunit().toString()))
					{
						comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
					}
					if (list.get(j).get("f_fullname").toString().equals(erpStockinventoryentryDto.getFBasicunit().toString()))
					{
						baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
					}
				}
				BigDecimal finalBaseConverRate = baseConverRate;
				BigDecimal finalComConverRate = comConverRate;
				erpStockinventoryentryList.forEach(erpStockinventoryentry ->{
					if(erpStockinventoryentryDto.getFGoodsid().equals(erpStockinventoryentry.getFGoodsid()))
					{
						//默认直接减
						erpStockinventoryentry.setFComdiff(erpStockinventoryentryDto.getFQuantity().subtract(erpStockinventoryentry.getFComamount()));
						erpStockinventoryentry.setFBasicdiff(erpStockinventoryentryDto.getFQuantity().subtract(erpStockinventoryentry.getFBasicamount()));

						if(!erpStockinventoryentryDto.getFBasicunit().equals(erpStockinventoryentryDto.getFUnitid()))
						{
							//已有库存减去现在库存
							erpStockinventoryentry.setFBasicdiff(erpStockinventoryentry.getFQuantity().multiply(finalBaseConverRate).subtract(erpStockinventoryentryDto.getFBasicamount()));
						}
						if(!erpStockinventoryentryDto.getFComunit().equals(erpStockinventoryentryDto.getFUnitid()))
						{
							//已有库存减去现在库存
							erpStockinventoryentry.setFComdiff(erpStockinventoryentry.getFQuantity().multiply(finalComConverRate).subtract(erpStockinventoryentryDto.getFComamount()));
						}
						BigDecimal o=new BigDecimal(0);
						if(erpStockinventoryentry.getFQuantity().compareTo(o)==0)
						{
							erpStockinventoryentry.setFComamount(o);
							erpStockinventoryentry.setFBasicamount(o);
						}
					}
				});
			});
		return  true;
	}
	@Override
	public boolean updateErpStockinventory(String id, ErpStockinventoryDto erpStockinventoryDto,ErpStockinventory erpStockinventory, List<ErpStockinventoryentryDto> erpStockinventoryentryDtos,List<ErpStockinventoryentry> erpStockinventoryentryList) {
		setDifQua(erpStockinventoryentryDtos,erpStockinventoryentryList);
		erpStockinventory.setFId(id);
		erpStockinventoryentryList.forEach(erpStockinventoryentry -> erpStockinventoryentry.setFInventoryid(id));
		erpStockinventoryentryMapper.delete(Wrappers.<ErpStockinventoryentry>query().lambda().eq(ErpStockinventoryentry::getFInventoryid, id));
		this.saveBatch(erpStockinventoryentryList, ErpStockinventoryentry.class, ErpStockinventoryentryMapper.class);
		return this.updateById(erpStockinventory);
	}

	public List<ErpStockinventoryentry> getErpStockinventoryentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpStockinventoryentry>query().lambda().eq(ErpStockinventoryentry::getFInventoryid, parentId);
		return erpStockinventoryentryMapper.selectList(wrapper);
	}

	public  List<ErpStockinventoryentryVo> getInfo(String F_InventoryId)
	{
		return  erpStockinventoryentryMapper.getInfo(F_InventoryId);
	}
}
